using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using umbraco;
using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.interfaces;
using umbraco.controls;
using Commerce.Common;

namespace CommerceForUmbraco.adminSection.commerce
{
    /// <summary>
    /// Edit Category
    /// </summary>
    public partial class editCategory : umbraco.BasePages.UmbracoEnsuredPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string categoryId = Request.QueryString.Get("c");
                Category thisCat = new Category(int.Parse(categoryId));
                CategoryName.Text = thisCat.CategoryName;
                shortDescription.Text = thisCat.ShortDescription;
                longDescription.Text = thisCat.LongDescription;
            }

            this.Panel1.Text = "Edit Category";
            this.pp_name.Text = "Category Name";
            this.pp_short_description.Text = "Short Description";
            this.pp_long_description.Text = "Long Description";
        }

        private void InitializeComponent()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            ImageButton button = this.Panel1.Menu.NewImageButton();
            button.Click += new ImageClickEventHandler(this.save_click);
            button.AlternateText = "Save";
            button.ImageUrl = GlobalSettings.Path + "/images/editor/save.gif";
            base.Load += new EventHandler(this.Page_Load);
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void save_click(object sender, ImageClickEventArgs e)
        {
            if (this.SaveCategory(this.CategoryName.Text, base.Request.QueryString["c"]))
            {
                base.speechBubble(0, ui.Text("speechBubbles", "commerceItemSaved"), "");
            }
            else
            {
                base.speechBubble(0, ui.Text("speechBubbles", "commerceItemErrorHeader"), ui.Text("speechBubbles", "commerceItemErrorText"));
            }
        }

        private bool SaveCategory(string categoryName, string categoryId)
        {
            try
            {
                Category cat = new Category(int.Parse(categoryId));
                cat.CategoryName = categoryName;
                cat.ShortDescription = shortDescription.Text;
                cat.LongDescription = longDescription.Text;
                cat.Save();

                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
 
